#!/bin/bash

# ANSI color codes
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

echo -e "${GREEN}Starting development environment...${NC}"

# Check if Docker is running
if ! docker info > /dev/null 2>&1; then
  echo -e "${RED}Error: Docker is not running. Please start Docker and try again.${NC}"
  exit 1
fi

# Start Docker Compose services
echo -e "${CYAN}Starting PostgreSQL and Redis services...${NC}"
docker compose -f docker-compose.dev.yaml up -d

# Wait for services to be ready
echo -e "${CYAN}Waiting for services to be ready...${NC}"
sleep 5

# Check if PostgreSQL is ready
if ! docker exec api-webservice-postgres pg_isready -U postgres > /dev/null 2>&1; then
  echo -e "${YELLOW}Warning: PostgreSQL might not be ready yet. You may need to wait a bit longer.${NC}"
else
  echo -e "${GREEN}PostgreSQL is ready!${NC}"
fi

# Check if Redis is ready
if [[ "$(docker exec api-webservice-redis redis-cli ping)" != "PONG" ]]; then
  echo -e "${YELLOW}Warning: Redis might not be ready yet. You may need to wait a bit longer.${NC}"
else
  echo -e "${GREEN}Redis is ready!${NC}"
fi

echo ""
echo -e "${GREEN}Development environment is set up!${NC}"
echo ""
echo -e "${CYAN}You can access the following services:${NC}"
echo -e "- PostgreSQL: localhost:5432 (Username: postgres, Password: postgres)"
echo -e "- Redis: localhost:6379"
echo -e "- Redis Commander: http://localhost:8081"
echo ""
echo -e "${CYAN}To start the NestJS application, run:${NC}"
echo -e "npm run start:dev"
echo ""
echo -e "${CYAN}To stop the development environment, run:${NC}"
echo -e "docker compose -f docker-compose.dev.yaml down" 